package com.android.music.prolyric;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.music.prolyric.database.DatabaseManger;
import com.android.music.prolyric.database.JSONParser;
import com.android.music.prolyric.database.LyricDTO;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class SaveLyricService extends IntentService{
	DatabaseManger data;
	
	
	public SaveLyricService() {
		super("");
		// TODO Auto-generated constructor stub
	}

	

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		data = new DatabaseManger(getBaseContext());
		
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Bundle b = intent.getExtras();
		if(b!= null)
		{
			String singerLocal = b.getString("singerLocal");
			HashMap<String, String> lyricToSave = (HashMap<String, String>) b.getSerializable("lyricToSave");
			
			int localSearch = b.getInt("localSearch");
			LyricDTO lyric = new LyricDTO();
			Log.d("savelyric localSearch", localSearch+"");
			lyric.setSongName(lyricToSave.get("songname"));
			lyric.setLyricEn(lyricToSave.get("lyricEn"));
			lyric.setLyricVi(lyricToSave.get("lyricVi"));
			lyric.setSinger(singerLocal);
			
			if(localSearch == 1)
				
				{
					Log.i("luu", "ok");
					
					if(!MusicPlayer.changeLyric)
						data.insertLyric(lyric);
					else
					{
						MusicPlayer.changeLyric=false;
						lyric.getStandarLyric();	
						data.updateLyric(lyric);
					}
				}
			else if(localSearch == 2)
			{
				Log.i("luu1", "ok");
				
				if(!MusicPlayer.changeLyric)
					{
						data.insertLyric(lyric);
						lyric.setSinger(lyricToSave.get("singer"));						
						data.insertLyricToHost(lyric);
					}
				else
				{
					MusicPlayer.changeLyric=false;
					lyric.getStandarLyric();	
					data.updateLyric(lyric);
				}
				
				
			}
			//insertLyricToHost(lyric);
			/*LyricDTO result = getLyricFromHost();
			if(result!= null)
			{
				Log.d("getFromHost", ""+result);
			}
			else {
				Log.d("getFromHost", "result is null");
			}*/
		}
		
		
	}
	
	
	
	



	

}
